import 'package:flutter/material.dart';
import 'package:power_house/scoped_model/auth.dart';
import 'package:power_house/util/scoped_model_helper.dart';

/// 个人信息页面的Header
var loginRegisterTextStyle = const TextStyle(fontSize: 20.0, color: Colors.white);

class Header extends StatelessWidget {
  const Header({Key? key}) : super(key: key);

  Widget _noLoginBuilder(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(left: 20.0),
      decoration: const BoxDecoration(color: Colors.green),
      height: 95.0,
      child: Row(
        children: [
          Container(
            width: 65.0,
            height: 65.0,
            margin: const EdgeInsets.only(right: 15.0),
            child: const CircleAvatar(
              backgroundImage: NetworkImage('https://s1.aigei.com/src/img/png/1f/1fc8f5bd42b54be5b28190bad9bf84b1.png?imageMogr2/auto-orient/thumbnail/!240x240r/gravity/Center/crop/240x240/quality/85/&e=1735488000&token=P7S2Xpzfz11vAkASLTkfHN7Fw-oOZBecqeJaxypL:1cCj281C0gVJBd1IyvYlx0cvCG0='),
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // Padding(padding: EdgeInsets.only(top: 6.0)),
              Row(
                children: [
                  GestureDetector(
                    onTap: () {
                      Navigator.of(context).pushNamed('login');
                    },
                    child: Text(
                      '登录',
                      style: loginRegisterTextStyle,
                    ),
                  ),
                  Text(
                    "/",
                    style: loginRegisterTextStyle,
                  ),
                  GestureDetector(
                    onTap: () {
                      Navigator.of(context).pushNamed('login');
                    },
                    child: Text(
                      '注册',
                      style: loginRegisterTextStyle,
                    ),
                  )
                ],
              ),
              GestureDetector(
                onTap: () {
                  Navigator.of(context).pushNamed('register');
                },
                child: const Text(
                  '登录可以体验更多内容',
                  style: TextStyle(color: Colors.white),
                ),
              )
            ],
          )
        ],
      ),
    );
  }

  Widget _loginBuilder(BuildContext context) {
    var userEntity = ScopedModelHelper.getModel<AuthModel>(context).user;
    String username = userEntity?.nickname ?? '获取用户名失败';
    String userImage = userEntity?.avatar ?? '';
    return Container(
      padding: const EdgeInsets.only(left: 20.0),
      decoration: const BoxDecoration(color: Colors.green),
      height: 95.0,
      child: Row(
        children: [
          Container(
            width: 65.0,
            height: 65.0,
            margin: const EdgeInsets.only(right: 15.0),
            child: CircleAvatar(
              backgroundImage: NetworkImage(userImage),
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // Padding(padding: EdgeInsets.only(top: 6.0)),
              Text(
                username,
                style: loginRegisterTextStyle,
              ),
              GestureDetector(
                onTap: () {
                  Navigator.of(context).pushNamed('register');
                },
                child: const Text(
                  '查看编辑个人资料',
                  style: TextStyle(color: Colors.white),
                ),
              )
            ],
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    var isLogin = ScopedModelHelper.getModel<AuthModel>(context).isLogin;
    if (isLogin) {
      return _loginBuilder(context);
    } else {
      return _noLoginBuilder(context);
    }
  }
}
